<?php 
/**
 * Manga Catcher
 */

class MangaCatcher
{
	const PIC_BASE_URI='http://t.jumpcn.com.cn/pcomic_com_cn/';
	const SESSION_KEY='MangaCatcher';

	private $data=array(
		'localBase'=>'',
		'remoteBase'=>'',
		'contentOfTable'=>array()
		
	);
	
	private $total;
	
	
	function __construct(){
		session_start();
		set_time_limit(60*60*12);
		$this->readBySession();
	}
	
	public function getProperty($key){
		if($this->checkPropertyExists($key)){
			return $this->data[$key];
		}
	}
	
	public function setProperty($key,$value){
		if($this->checkPropertyExists($key)){
			$this->data[$key]=$value;
		}			
	}
	
	public function checkPropertyExists($key){
		return array_key_exists($key,$this->data)||die('property '.$key.' not defind');
	}
	
	public function saveToSession(){
		$_SESSION[SESSION_KEY]=$this->data;
	}

	public function readBySession(){
		if($_SESSION[SESSION_KEY]){
			$this->data=$_SESSION[SESSION_KEY];
		}
	}
	
	public function getContentOfTable($url){
		$html=file_get_contents($url);
		preg_match_all('/<a href="\d+\/" target="_blank">(.*?)<\/a>/i', $html,$result);
		
		foreach($result[0] as $link){
			preg_match('/\="(\d+)\/"/',$link,$id);
			preg_match('/>([^<>]*)</',$link,$title);
			$id=$id[1];
			$title=$title[1];
			$r[]=array('id'=>$id,'title'=>$title);
		}
		/*
		$sortfun=function($a,$b){
			return (intval($a["id"])<intval($b["id"]))?-1:1;
		};
		*/
		$sortfun=create_function('$a,$b','return (intval($a["id"])<intval($b["id"]))?-1:1;');
		usort($r,$sortfun);
		$this->setProperty('contentOfTable',$r);
		$this->setProperty('remoteBase',$url);
		return $r;
	}
	
	public function expandChapter(){
		$contentOfTable=$this->getProperty('contentOfTable');
		$remoteBase=$this->getProperty('remoteBase');
		$total=$this->total;
		
		for($i=0,$len=count($contentOfTable);$i<$len;$i++){
			$sar=$contentOfTable[$i];
			$jshref=$remoteBase.$sar['id'].'/index.js';
			$js=file_get_contents($jshref);
			preg_match('/var\s+total=(\d+)/',$js,$pageTotal);
			$pageTotal=$pageTotal[1];
			preg_match('/var\s+volpic=([\'"])([^\1]*)\1/',$js,$picDir);
			$picDir=$picDir[2];
			preg_match('/var\stpf\s*\=\s*(\d)/',$js,$nameType);
			$nameType=$nameType[1];
			
			
			$contentOfTable[$i]['picDir']=$picDir;
			$contentOfTable[$i]['pages']=$pageTotal;
			$contentOfTable[$i]['nameType']=$nameType;
			$total+=$pageTotal;			
		}
		$this->total=$total;
		$this->setProperty('contentOfTable',$contentOfTable);
		return $contentOfTable;
	}
	
	public function getImage($url,$path,$filename){
		$this->createFolder($path);
		@$image=file_get_contents($url);
		$file=file_put_contents($path.$filename,$image);
	}
	
	public function createFolder($path){
		if(!file_exists($path)){
			$this->createFolder(dirname($path));
			mkdir($path,'777');
		}
	}
	
	public function progress($cur){
		$total=$this->total;
		return $cur/$total;
		//return sprintf('%.2f',$cur/$total*100);
	}
	
	public function serialize(){
		$localBase=$this->getProperty('localBase');
		$serializeDir=$localBase.'-serialize';
		$handler=dir($localBase);
		$this->createFolder($serializeDir);
		while($entry=$handler->read()){
			if($entry=='.'||$entry=='..'){
				continue;
			}
			preg_match('/\d+/',$entry,$num);
			$num=$num[0];
			
			if(strpos($num,'0')===0){
				$prefix=sprintf('%04d',$num);
			}
			else{
				$prefix=sprintf('%03d',$num);
			}
			
			$this->walkPicture($localBase.'/'.$entry.'/' , $serializeDir , $prefix.'-');
		}
		
	}
	
	public function walkPicture($oldDir,$newDir,$prefix){
		if($resource=opendir($oldDir)){
			while($pic=readdir($resource)){
				if($pic=='.'||$pic=='..'){
					continue;
				}
				$newName=sprintf('%03d',$pic);
				copy($oldDir.$pic,$newDir.'/'.$prefix.$pic.'.jpg');
			}
		}
	}
	
	
}

?>
